<?php

class google_analyticsActions extends sfActions {

    public function executeIndex(sfWebRequest $request){
        $myLib = new myLib();
        $myGoogleAnalytics = new myGoogleAnalytics();

        /* valida que el usuario este conectado */
        $myLib->validateAuthentication("Google");
        
        $current_user = $this->getUser()->getAttribute('current_user');
        $refresh_token = $myGoogleAnalytics->getSavedRefreshToken($current_user);
        $access_token = $myGoogleAnalytics->useRefreshToken($refresh_token);
/*
        $hybridauth = new Hybrid_Auth($config);
        $google = $hybridauth->authenticate( "Google" );
        $data = $google->api()->api( "https://www.googleapis.com/analytics/v3/management/accounts" );*/


        $google_accounts = $myGoogleAnalytics->getAccounts($access_token);

        $this->google_accounts = $google_accounts;
    }

    public function executeAjax_search(sfWebRequest $request){
        $myLib = new myLib();
        $myGoogleAnalytics = new myGoogleAnalytics();

        $myLib->validateAuthentication("Google");

        $account = $request->getParameter('account');
        $metric = $request->getParameter('metric');
        $date_from = $request->getParameter('date_from');
        $date_to = $request->getParameter('date_to');

        $data = "";

        $current_user = $this->getUser()->getAttribute('current_user');
        $refresh_token = $myGoogleAnalytics->getSavedRefreshToken($current_user);

        $data = $myGoogleAnalytics->getData($refresh_token, $account, $metric, $date_from, $date_to);

        echo "Data obtenida desde google analytics: ".$data;
        return sfView::NONE;
    }

}